﻿#include "projectHeader.h"

void addStudentInfo ( student **stu )
{
    student *walkStuTemp = NULL;
    walkStuTemp = ( student * ) malloc ( sizeof ( student ) );

    printf ( "Give student's name: \n" );
    scanf ( "%s", walkStuTemp->cName );
    printf ( "Give student's score: \n" );
    scanf ( "%lf", &walkStuTemp->nScore );
    walkStuTemp->next = *stu;
    (*stu) = walkStuTemp;
    nCountStu ++;
    /* add a student */
}

void changeStudentInfo ( student **stu )
{
    char cName[64];
    student *walkStuTemp, *holdStuTemp, *stuTemp;
    makeStuEmpty ( &stuTemp );

    printf ( "Enter the student's name: \n" );
    scanf ( "%s", cName );

    if ( isStudent ( cName, *stu ) )
    {
        for ( holdStuTemp = (*stu);
              strcmp ( holdStuTemp->cName, cName );
              holdStuTemp = holdStuTemp->next )
        {
            walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
            strcpy ( walkStuTemp->cName, holdStuTemp->cName );
            walkStuTemp->nScore = holdStuTemp->nScore;
            walkStuTemp->next = stuTemp;
            stuTemp = walkStuTemp;
        }
        /* pop out info and push into temp */

        printf ( "Enter new name: \n" );
        scanf ( "%s", holdStuTemp->cName );
        printf ( "Enter new score: \n" );
        scanf ( "%lf", &holdStuTemp->nScore );
        /* change info */

        for ( stuTemp = stuTemp->next; stuTemp != NULL; stuTemp = stuTemp->next )
        {
            walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
            strcpy ( walkStuTemp->cName, stuTemp->cName );
            walkStuTemp->nScore = stuTemp->nScore;
            walkStuTemp->next = holdStuTemp;
            holdStuTemp = walkStuTemp;
        }
        /* push back to hold */

        (*stu) = holdStuTemp;
        printf ( "Change successful!\n" );
    }
    else
        printf ( "Warning: Did not find this instructor\n" );
}

void clearStudent ( student **stu )
{
    int i;
    student *dltTemp;

    for ( i = 0; i < nCountStu; i ++ )
    {
        dltTemp = (*stu);
        (*stu) = (*stu)->next;
        free ( dltTemp );
    }
}
/* clear student */

void deleteStudent ( student **stu )
{
    char cName[9];
    student *walkStuTemp, *holdStuTemp, *dltTemp, *stuTemp;
    makeStuEmpty ( &holdStuTemp );
    makeStuEmpty ( &stuTemp );

    printf ( "Enter the student name: " );
    scanf ( "%s", cName );

    if ( isStudent ( cName, *stu ) )
    {
        for ( holdStuTemp = (*stu);
              strcmp ( holdStuTemp->cName, cName );
              holdStuTemp = holdStuTemp->next )
        {
            walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
            strcpy ( walkStuTemp->cName, holdStuTemp->cName );
            walkStuTemp->nScore = holdStuTemp->nScore;
            walkStuTemp->next = stuTemp;
            stuTemp = walkStuTemp;
        }
        /* pop out info and push into temp */

        dltTemp = holdStuTemp;
        holdStuTemp = holdStuTemp->next;
        free ( dltTemp );
        nCountStu --;
        /* delete student */

        for ( stuTemp = stuTemp->next; stuTemp != NULL; stuTemp = stuTemp->next )
        {
            walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
            strcpy ( walkStuTemp->cName, holdStuTemp->cName );
            walkStuTemp->nScore = holdStuTemp->nScore;
            walkStuTemp->next = holdStuTemp;
            holdStuTemp = walkStuTemp;
        }
        /* push back to hold */

        (*stu) = holdStuTemp;
        printf ( "Delete successful!\n" );
    }
    else
        printf ( "Warning: Did not find this student\n" );
}

int isStudent ( char *c , student *stu )
{
    int i = 0;
    char cTemp[9];

    for ( i = 0;
          strcmp ( cTemp, c ) && i < nCountStu;
          stu = stu->next )
    {
        strcpy ( cTemp, stu->cName );
        i ++;
    }
    /* find student */

    if ( strcmp ( cTemp, c ) )
        return 0;
    else
        return 1;
}

void getStudentScore ( char *c, student *stu )
{
    int i;
    double nScore;
    char cTemp[64];

    for ( i = 0;
          strcmp ( cTemp, c ) && i < nCountStu;
          i ++ )
    {
        nScore = stu->nScore;
        strcpy ( cTemp, stu->cName );
        stu = stu->next;
    }
    /* find student */

    if ( strcmp ( cTemp, c ) )
        printf ( "Score error: Student %s not found\n", c );
    else
        printf ( "The score for student %s is %.1lf\n", c, nScore );
    /* return result */
}

void makeStuEmpty ( student **stu )
{
    student *walkStuTemp;
    walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
    strcpy ( walkStuTemp->cName, "" );
    walkStuTemp->next = NULL;
    (*stu) = walkStuTemp;
}
/* make student empty */

student *readStudents ( FILE *f )
{
    student *walkStuTemp, *holdStuTemp;
    int i;

    makeStuEmpty ( &holdStuTemp );
    fscanf ( f, "%d", &nCountStu );

    for ( i = 0; i < nCountStu; i ++ )
    {
        walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
        fscanf ( f, "%s %lf", walkStuTemp->cName, &walkStuTemp->nScore );
        walkStuTemp->next = holdStuTemp;
        holdStuTemp = walkStuTemp;
    }

    return holdStuTemp;
}

void sortStudents ( FILE *f, student *stu )
{
    int i, j, nHold, nWalk;
    double score[nCountStu];
    char name[nCountStu][64];
    char cNameHold[64];
    student *holdStuTemp = stu;

    for ( i = 0; i < nCountStu; i ++ )
    {
        score[i] = holdStuTemp->nScore;
        strcpy ( name[i], holdStuTemp->cName );
        holdStuTemp = holdStuTemp->next;
    }
    /* pop from structures to array */

    for ( i = nCountStu / 2; i != 0; i /= 2 )
    {
        for ( j = i; j < nCountStu; j++ )
        {
            nHold = score[j];
            strcpy ( cNameHold, name[j] );

            for ( nWalk = j - i;
                  nWalk >= 0 && nHold < score[nWalk]; nWalk -= i )
            {
                score[nWalk+i] = score[nWalk];
                strcpy ( name[nWalk+i], name[nWalk] );
            }

            score[nWalk+i] = nHold;
            strcpy ( name[nWalk+i], cNameHold );
        }
    }
    /* Shell sort */

    for ( i = nCountStu - 1; i >= 0; i -- )
        fprintf ( f, "%s %.1lf\n", name[i], score[i] );
    /* save students' info */
}
